<template>
  <div class="home">
    <el-container class="main-container" style="height: 100vh; overflow: hidden;">
      <el-header class="header">
        <div class="left">
          <!--头像-->
          <img src="@/assets/joker.png">
          <span>
            后台管理系统
          </span>
        </div>
        <div class="right">
          <!-- 退出按钮 -->
          <el-button type="danger" @click="logout">退出</el-button>
        </div>
      </el-header>
      <el-container class="content-container">
        <!--导航栏-->
        <el-aside class="aside" :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse">
            <span v-if="!isCollapse">
              <img v-if="!isCollapse" alt="Image" src="@/assets/icon/收起.png" style="max-width: 80%; max-height: 80%;">
            </span>
            <span v-else>
              <el-tooltip class="item" effect="white" content="展开导航栏" placement="right">
                <img alt="Image" src="@/assets/icon/展开.png" style="max-width: 80%; max-height: 80%;">
              </el-tooltip>
          </span>
          </div>
          <el-menu

              default-active="1"
              :router="true"
              class="el-menu-vertical-demo"
              :collapse="isCollapse"
              :unique-opened="true">
            <!-- 一级菜单 -->
            <el-submenu v-for="(item, index) in menuData" :key="index" :index="item.url">
              <template #title>
                <i :class="item.icon"></i>
                <span>{{ item.name }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item v-for="(child, childIndex) in item.children" :key="childIndex" :index="child.url">
                <i :class="child.icon"></i>
                <span>{{ child.name }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <!--主要展示部分-->
        <el-container class="main-content">
          <el-main style="height: calc(100vh - 60px); overflow: auto;">
            <router-view></router-view>
          </el-main>
        </el-container>

      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  created() {
    const employeeId = this.$route.query.employeeId;
    // 根据员工信息进行查询菜单
    this.fetchMenuList(employeeId);

    // 检查本地存储是否存在菜单数据
    const storedMenuData = window.sessionStorage.getItem('menuData');
    if (storedMenuData) {
      this.menuData = JSON.parse(storedMenuData);
    }
  },
  data() {
    return {
      isCollapse: false,
      menuData: ''
    }
  },

  name: "Home",
  methods: {
    logout() {
      // 清空sessionStorage
      window.sessionStorage.clear();
      // 退回到登录页，路由跳转
      this.$router.push("/login");
    },
    // 控制菜单展开和折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    async fetchMenuList(employeeId) {
      const storedMenuData = window.sessionStorage.getItem('menuData');
      if (!storedMenuData) {
        try {
          const response = await this.$http.post(`/community/menu/` + employeeId);
          this.menuData = response.data;

          // 将菜单数据保存到本地存储
          window.sessionStorage.setItem('menuData', JSON.stringify(this.menuData));
        } catch (error) {

        }
      }
    }
  }
};
</script>

<style scoped>
.home {
  height: 100vh;
}

.main-container {
  height: 100%;
}

.aside {
  background-color: #7DCEA0;
  height: auto;
}

.main-content {
  background-color: #f5f5f5;
  height: 100%;
}

.main {
  background-color: #f8f8f8;
  height: 100%;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  background-color: #7DCEA0;
}

.left {
  display: flex;
  align-items: center;
}

.left img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 2px solid #007bff;
  box-sizing: border-box;
}

.right {
  display: flex;
  align-items: center;
}

.toggle-button {
  background-color: aliceblue;
  color: #007bff;
  text-align: center;
  font-size: 12px;
  letter-spacing: 0.5em;
  height: 30px;
  line-height: 30px; /* 添加这一行 */
}


</style>